package com.example.g32.park

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.activity.viewModels
import android.widget.AdapterView.OnItemSelectedListener
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.ParkEntityItem
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.UiState
import com.example.g32.R
import com.example.g32.databinding.ActivityParkAddBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/app/ParkAddActivity")
@AndroidEntryPoint
class ParkAddActivity : BaseActivity<ActivityParkAddBinding, ParkIntent, ParkViewModel>() {
    override val binding by lazy {
        ActivityParkAddBinding.inflate(layoutInflater)
    }
    override val vm by viewModels<ParkViewModel>()
    var areaId=1
    var areaName="A区停车场"
    var status="空闲"
    var type="标准车位"
    override fun initView() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*>->{
                        ToastUtils.showLong("添加成功")
                        finish()
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initData() {
        binding.headParkadd.apply {
            headBack.setOnClickListener {
                finish()
            }
            headTitle.setText("车位添加")
            headAdd.setImageResource(R.drawable.ic_action_ok)
            headAdd.setOnClickListener {
                val carNum = binding.parkaddCarNo.text.toString()
                val remark = binding.parkaddRemark.text.toString()
                val item =
                    ParkEntityItem(areaId, areaName, null, "", remark, carNum, status, type, 0, "")
                vm.sendIntent(ParkIntent.addPark(item))
            }
        }
        binding.parkaddRemark.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                val length = s?.length
                binding.parkaddNum.setText("${length}/10")
                if (length == 10) {
                    binding.parkaddNum.setTextColor(Color.RED)
                } else {
                    binding.parkaddNum.setTextColor(Color.DKGRAY)
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }

        })

        val areaList = arrayOf("A区停车场", "B区停车场", "C区停车场")
        val areaAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, areaList)
        areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkaddArea.adapter = areaAdapter
        binding.parkaddArea.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                areaId = position + 1
                areaName = areaList[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

        }


        val typeList = arrayOf("标准车位", "VIP车位")
        val typeAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, typeList)
        typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkaddType.adapter = typeAdapter
        binding.parkaddType.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                type = typeList[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

        }

        binding.pardaddRg.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.pardadd_kx ->
                    status = "空闲"

                R.id.pardadd_zy ->
                    status = "已占用"

                R.id.pardadd_wh ->
                    status = "维护中"
            }
        }
    }
}